package com.java.design.spring.context.support;

import com.java.design.spring.beans.factory.support.BeanDefinitionReader;
import com.java.design.spring.beans.factory.support.BeanDefinitionRegistry;
import com.java.design.spring.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractApplicationContext implements ApplicationContext {
    //声明解析器变量
    protected BeanDefinitionReader beanDefinitionReader;
    //定义用于存储bean对象的map 容器protected
    Map<String, Object> singletonobjects = new HashMap<String, Object>();
    //声明配置文件路径的变量
    protected String configLocation;

    public void refresh() throws Exception {
        //加载BeanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        //初始化bean
        finishBeanInitialization();
    }

    //bean的初始化
    private void finishBeanInitialization() throws Exception {//获取注册表对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        //获取BeanDefinition对象
        String[] beanNames = registry.getBeanDefinitionNames();
        for (String beanName : beanNames) {//进行bean的初始化
            getBean(beanName);
        }
    }
}